package org.test.pong.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.test.pong.utils.RateLimiterDiy;
import reactor.core.publisher.Mono;

/**
 * @ClassName PongController
 * @Description TODO
 * @Author JiaLin
 * @Date 2024/11/20 17:29
 */
@Slf4j
@RestController
public class PongController {

    private final RateLimiterDiy rateLimit = new RateLimiterDiy();
    @GetMapping("/pong")
    public Mono<ResponseEntity<String>> pong() {

        //Pong服务限1个请求
        if (rateLimit.tryInvoke()){
            log.info("Pong Response World");
            return Mono.just(ResponseEntity.ok("Pong Response World"));
        }else{
            log.info("429 Too Many Requests");
            return Mono.just(ResponseEntity.status(HttpStatus.TOO_MANY_REQUESTS).body("429 Too Many Requests"));
        }

    }

}


